package com.itheima.web.controllor.store.company;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.company.Company;
import com.itheima.service.store.company.CompanyService;
import com.itheima.service.store.company.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns="/store/company")
public class CompanyServlet extends HttpServlet {
    private CompanyService companyService = new CompanyServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            //进入企业管理页面
          this.list(req,resp);
        }else if ("toAdd".equals(operation)){
            //进入添加页面
            this.toAdd(req,resp);
        }else if ("save".equals(operation)){
            this.save(req,resp);
        }else if ("toEdit".equals(operation)){
            this.toEdit(req,resp);
        }else if ("edit".equals(operation)){
            this.update(req,resp);
        }else if("delete".equals(operation)){
            this.delete(req,resp);
        }
    }
/*
删除操作
 */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Company company = BeanUtil.fillBean(req, Company.class);

        int i = companyService.delete(company);
        System.out.println(i);
        resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");

    }

    /**
     提交修改参数，修改操作
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
        String id = req.getParameter("id");
//        String id ="1";
//        company.setId(id);
        companyService.update(company);
        //跳转list页面
        resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");

    }

    /**
     * 进入修改页面
     */
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取修改项的id
        String id = req.getParameter("id");
        //根据id查询单个对象
        Company company = companyService.findById(id);
        req.setAttribute("company",company);
        req.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(req,resp);
    }

    /**
   提交添加参数，增加操作
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Company company = BeanUtil.fillBean(req, Company.class,"yyyy-MM-dd");
        System.out.println(company);
        int rst = companyService.save(company);
        System.out.println(rst);
            //添加成功，回到列表页面
           // list(req, resp);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");

    }

    /**
     * 进入添加页面
     */
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /*
    进入列表显示页面
     */
    public  void list(HttpServletRequest req, HttpServletResponse resp){
        //进入list页面-->设置页面的当前页和每页显示条数
        int currentPage=1;
        int pageSize = 5;
        //判断客户端传递的参数有没有带当前页和每页条数，如果改变了，给默认值重新赋值
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        if (StringUtils.isNotBlank(page)){
            currentPage=Integer.parseInt(page);
        }
        if (StringUtils.isNotBlank(size)){
            pageSize=Integer.parseInt(size);
        }
        //当传递的当前页和每页显示条数为空时，查询所有信息在同一页
        PageInfo all = companyService.findAll(currentPage, pageSize);
        req.setAttribute("page",all);

        //跳转到list页面
        try {
            req.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
